package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增地址
     * @author Mosetian
     * @date 2022-9-16 8:44
     * @param	addressBook
     * @return R
     */
    @PostMapping
    public R save(@RequestBody AddressBook addressBook){
        addressBook.setUserId(UserHolder.getUserId());
        boolean save = addressBookService.save(addressBook);
        return save ? R.success("保存地址成功") : R.error("保存地址失败");
    }

    /**
     *设置默认地址
     * @author Mosetian
     * @date 2022-9-16 9:00
     * @param	addressBook
     * @return R
     */
    @PutMapping("/default")
    public R setDefaultAddress(@RequestBody AddressBook addressBook){

        LambdaUpdateWrapper<AddressBook> qw = new LambdaUpdateWrapper<>();
        qw.set(AddressBook::getIsDefault, 0);
        qw.eq(AddressBook::getUserId, UserHolder.getUserId()); // where user_id =?

        addressBookService.update(qw);

        addressBook.setIsDefault(1);

        boolean flag = addressBookService.updateById(addressBook);

        return flag ? R.success("设置成功") : R.error("设置失败");
    }

    /**
     * 根据ID查询地址
     * @author Mosetian
     * @date 2022-9-16 11:28
     * @param	id
     * @return R
     */
    @GetMapping("/{id}")
    public R getAddressById(@PathVariable Long id){
        AddressBook addressBook = addressBookService.getById(id);
        if (addressBook != null) {
            return R.success(addressBook);
        } else {
            return R.error("没有找到该对象");
        }
    }

    /**
     * 查询默认地址
     * @author Mosetian
     * @date 2022-9-16 11:29
     * @param
     * @return R<AddressBook>
     */
    @GetMapping("/default")
    public R<AddressBook> getDefaultAddress(){
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId, UserHolder.getUserId());
        qw.eq(AddressBook::getIsDefault, 1);

        AddressBook addressBook = addressBookService.getOne(qw);

        if (addressBook == null) {
            return R.error("没有找到该对象");
        } else {
            return R.success(addressBook);
        }
    }

    /**
     *查询指定用户的全部地址
     * @author Mosetian
     * @date 2022-9-16 13:04
     * @param	addressBook
     * @return R
     */
    @GetMapping("/list")
    public R getAllAddressById(AddressBook addressBook){
        addressBook.setUserId(UserHolder.getUserId());

        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(addressBook.getUserId() != null, AddressBook::getUserId, addressBook.getUserId());

        List<AddressBook> list = addressBookService.list(qw);

        return R.success(list);
    }

    /**
     * 删除地址【可能是多个】
     * @author Mosetian
     * @date 2022-9-16 13:13
     * @param	ids
     * @return R
     */
    @DeleteMapping
    public R deleteByIds(@RequestParam List<Long> ids){

        boolean flag = addressBookService.removeByIds(ids);

        return flag ? R.success("删除成功") : R.error("删除失败");
    }

    /**
     * 修改地址
     * @author Mosetian
     * @date 2022-9-16 14:29
     * @param	addressBook
     * @return R
     */
    @PutMapping
    public R updateAddress(@RequestBody AddressBook addressBook){

        boolean flag = addressBookService.updateById(addressBook);

        return flag ? R.success("修改成功") : R.error("修改失败");
    }
}
